package com.readnote.common.action;

import com.cr.backbone.action.AppBaseActionSupport;
import com.cr.backbone.action.LoginInfo;
import com.cr.backbone.intercepter.SessionInterceptKeys;
import com.opensymphony.xwork2.ActionContext;
import com.readnote.user.model.User;
import com.readnote.user.model.UserInfo;
import com.readnote.user.service.UserInfoService;
import com.readnote.user.service.UserService;


public class RegisterUserAction extends AppBaseActionSupport {
	
	private static final String INDEX = "index";
	private String message;
	private User user;
	private UserService userService;
	private UserInfoService userInfoService;
	
	public String index(){
		return INPUT; 
	}
	
	public String save(){
		//1 空验证
		//2 用户不重复验证
		if(userService.getUserByLoginName(user.getLoginName()) !=null ){
			addFieldError("user.loginName","用户"+user.getLoginName()+"已存在");
			return INPUT;
		}
		
		//3 注册处理
		userService.saveRegisterUser(user);
		
		//4 恭喜用户，并自动跳转到个人中心
		message="恭喜您，注册成功！";
		setLoginInfoSetInSession(user);
		return SUCCESS;
	}

	/**
	 * 
	 * 登录信息LoginInfo，加载到session中
	 * @param _user
	 */
	private void setLoginInfoSetInSession(User _user){
		// 登录信息
		UserInfo _userInfo=userInfoService.getUserInfoByUser(_user);
		LoginInfo loginInfo=new LoginInfo();
		loginInfo.login(_user, _userInfo);
		ActionContext.getContext().getSession().put(SessionInterceptKeys.LOGIN_USER_LOGIN_INFO_KEY, loginInfo);
	}
	
	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public UserInfoService getUserInfoService() {
		return userInfoService;
	}

	public void setUserInfoService(UserInfoService userInfoService) {
		this.userInfoService = userInfoService;
	}
}
